#ifndef _SMART_CURSOR
#define _SMART_CURSOR
class SmartCursor{
public:
//    Dbc *cursor;
    RowHash *rh;
    IndexDbS *idbs;
    IndexDbI *idbi;
    RHIter rhit;
    IndexDbS::Iter sit;
    IndexDbI::Iter iit;
    IndexDbS::Iter str_end;
    IndexDbI::Iter int_end;
	//vector<_byte*> mit;
	size_t mit;
	//DB::Cursor *cursorp;
	MyDb *_db;

    //IndexDbS::UM_Iter um_sit;
	//IndexDbS::UM_Iter um_iit;

    bool isFirst;
    int type;
    DataType data_type;

    
    SmartCursor(MyDb * db, int t_type){
        type = t_type;
		_db = db;
        //db->cursor(NULL, &cursor, 0);
		//cursorp = db->cursor();
		//assert(cursorp);
		//cursorp->jump();
		mit = 1;
	    isFirst = true;
    }
    OBJID getNextID(){
        int ret;
        OBJID tmp;
        switch(type){
        case 0:{
            //Dbt key,data;
		    if(mit<_db->getRowCount()+1){
			  return (mit++);
			}else return 0;//NOTFOUND
        }
        case 1:
            if(rhit!=rh->end()){
                tmp = *rhit; 
                ++rhit;
                return tmp;
            }else{
                return 0;
            }
        case 5: // string
              
        case 6:  // int
                        
        case 7:            // hashtable
       case 2:
         case 3:
				  assert(0);
		}
                   return 0;
    }
    SmartCursor(RowHash *_rh, int t_type):rh(_rh),rhit(_rh->begin()),type(t_type),isFirst(true){}
    SmartCursor(IndexDbS *_idb, int t_type):idbs(_idb),sit(_idb->data.begin()),type(t_type),isFirst(true){}
    SmartCursor(IndexDbI *_idb, int t_type):idbi(_idb),iit(_idb->data.begin()),type(t_type),isFirst(true){}

    void closeCursor(){
    }
protected:
    SmartCursor(){};
};

class SmartTable{
public:
    MyDb *db;
    RowHash *rh;
    IndexDbS *idbs;
    IndexDbI *idbi;
    MyDb *parent;
    /* qjp */
    int index;
    int type;
    SmartTable(MyDb *_db, MyDb *p, int _index):parent(p),type(0),db(_db),index(_index) { }
    SmartTable(RowHash *_db, MyDb *p, int _index, bool flag=false):parent(p),rh(_db),index(_index){
        if (!flag) {
            type = 1;
        }
        else {
            type = 7;
        }
    }
    SmartTable(IndexDbS *_db, MyDb *p, int _index, bool flag=false):parent(p),idbs(_db),index(_index) {
        if (!flag) {
            type = 2;
        } else {
            type = 5;
        }
    }
    SmartTable(IndexDbI *_db, MyDb *p, int _index, bool flag=false):parent(p),idbi(_db),index(_index) {
        if (!flag) {
            type = 3;
        } else {
            type = 6;
        }
    }

    MyDb * getPDb(){
        return parent;
    }

    SmartCursor getCursor(){
        switch(type){
        case 0:
            return SmartCursor(db, type);
        case 1:
        case 7:
            return SmartCursor(rh, type);
        case 2:
        case 5:
            return SmartCursor(idbs, type);
        case 3:
        case 6:
            return SmartCursor(idbi, type);
        }
    }
    int getRowCount() const {
        cout << "type : " << type << endl;
        switch(type) {
        case 0: 
        case 2:
        case 5:
        case 3:
        case 6: {
            assert(parent != NULL);
            //cout << parent->getRowCount() << endl;
            return parent->getRowCount();
        }
        case 1:
        case 7: {
            return rh->size();
        }
        }
    }
};




#endif
